package com.tech4u.edu4u_demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tech4u.edu4u_demo.R;
import com.tech4u.edu4u_demo.entity.ChatDetail;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<ChatDetail> list;
    Context context;

    public MyAdapter(List<ChatDetail> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == 1){
           View view =  LayoutInflater.from(context).inflate(R.layout.item_left, parent, false);
           return new MyHolderOrder(view);
        }else{
            View view =  LayoutInflater.from(context).inflate(R.layout.item_right, parent, false);
            return new MyHolderReverseOrder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof MyHolderOrder){
            if (list.get(position).getType() == 1){
                ((MyHolderOrder) holder).tv_content.setText(list.get(position).getWord());
            }else{
                ((MyHolderOrder) holder).tv_content.setText("语音>>>>>" + list.get(position).getWord());
            }
        }else{
            if (list.get(position).getType() == 1){
                ((MyHolderReverseOrder) holder).tv_content.setText(list.get(position).getWord());
            }else{
                ((MyHolderReverseOrder) holder).tv_content.setText("语音>>>>>" + list.get(position).getWord());
            }

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        /** 1: 顺序, -1: 倒序*/
        return list.get(position).isSortOrder() ? 1 : -1;
    }

    /**
     * 顺序
     */
    public static class MyHolderOrder extends RecyclerView.ViewHolder {

        ImageView iv_avatar;
        TextView tv_name, tv_content;

        public MyHolderOrder(@NonNull View itemView) {
            super(itemView);

            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_content = itemView.findViewById(R.id.tv_content);
        }
    }


    /**
     * 倒序
     */
    public static class MyHolderReverseOrder extends RecyclerView.ViewHolder {
        ImageView iv_avatar;
        TextView tv_name, tv_content;
        public MyHolderReverseOrder(@NonNull View itemView) {
            super(itemView);
            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_content = itemView.findViewById(R.id.tv_content);
        }
    }
}
